/**
 * PsiFramework
 * Php Simple Framework
 * 
 * @author Rémi San
 * @version beta
 */

/**
 * Fonctions d'autocompletion
 */
function autoComplete(field, service){
	if(field != null){
		new Ajax.Request('index.php', 
			{
				method:'post',
				encoding: 'UTF-8',
				parameters: 'service=' + service + '&value=' + field.value,
				onLoading: function(){loading();},
				onSuccess: function(transport){success(field, transport);},
				onFailure: function(){failure();},
				onComplete: function(){endLoading();}
			}
		);
	}
}

function buildChoiceList(field, xml){
	var values = xml.getElementsByTagName("value") ;
	var listId = field.attributes.name.value + "_autoCompleteList" ;
	
	document.getElementById(field.attributes.name.value + "_id").value = "" ;
	
	var div = document.getElementById(listId) ;
	if(div == null){
		div = document.createElement("div") ;
		div.setAttribute("id", listId) ;
		div.className = "autoCompleteDiv" ;
		div.style.position = "absolute" ;
		div.style.width = 200 ;
		div.style.height = 240 ;
		div.style.overflow = 'auto' ;
		div.style.marginLeft = field.offsetLeft;
		div.style.top = field.offsetTop + field.clientHeight + 4 ;
		div.field = field;
		div.onmouseover = function(){ this.field.mouseInAutoComplete = true ; } ;
		div.onmouseout = function(){ this.field.mouseInAutoComplete = false ; } ;
		
		field.mouseInAutoComplete = false;
		field.autoCompleteDiv = div ;
		field.onblur= function() { hideAutoComplete(this) ; } ;
	}else{
		div.innerHTML = "" ;
	}
	div.style.display = 'block' ;
	
	
	var ul = document.createElement("ul") ;
	ul.className = "autoCompleteList" ;
	ul.style.width = field.style.width ;
		
	for(var i=0; i<values.length; i++){
		var value = values[i] ;
		var li = document.createElement("li") ;
		li.innerHTML = value.textContent ;
		li.valueId = value.attributes.id.value ;
		li.container = div ;
		li.onclick = function() { select(field, this) ; } ;
		if(i%2==0) li.className = "autoCompleteElement autoCompleteEven" ;
		else li.className = "autoCompleteElement autoCompleteOdd" ;
		ul.appendChild(li) ;
	}
	
	if(values.length == 0){
		hideAutoComplete(field) ;
	}
	else{
		div.appendChild(ul);
		field.parentNode.appendChild(div) ;
	}
}

function hideAutoComplete(field){
	if(!field.mouseInAutoComplete) field.autoCompleteDiv.style.display = 'none' ;
}

function select(field, element){
	field.value = element.innerHTML ;
	document.getElementById(field.attributes.name.value + "_id").value = element.valueId ;
	field.mouseInAutoComplete = false ;
	hideAutoComplete(field) ;
}

/**
 * The ajax onSuccess function
 * 
 * @param field
 * @param transport
 * @return
 */
function success(field, transport){
	if(field!=null){
		buildChoiceList(field, transport.responseXML) ;
	}
}
 
/**
 * The ajax onLoading action
 */
function loading(){
}

/**
 * The ajax onComplete action 
 */
function endLoading(){
}

/**
 * The Ajax onFailure action
 */
function failure(){
         alert('Request error !') ;
}

/***********************************************************
 ***********************************************************
 ***********************************************************/

function closeDebug(){
	var debugContainer = document.getElementById('debugContainer') ;
	debugContainer.className = 'reduced' ;
	
	var debug = document.getElementById('debug') ;
	debug.className = 'hidden' ;
	
	var debugControl = document.getElementById('debugControl') ;
	debugControl.onclick = function(){ showDebug() ; } ;
}

function showDebug(){
	var debugContainer = document.getElementById('debugContainer') ;
	debugContainer.className = 'expanded' ;
	
	var debug = document.getElementById('debug') ;
	debug.className = 'visible' ;
	
	var debugControl = document.getElementById('debugControl') ;
	debugControl.onclick = function(){ closeDebug() ; } ;
}

/***********************************************************
 ***********************************************************
 ***********************************************************/

function getEmptyAttribute(){
	return getAttribute("", "") ;
}

function getAttribute(name, type){
	return { "name" : name, "type" : type } ;
}

function addNewAttribute(parent, listOfTypes, index){
	addAttribute(parent, getEmptyAttribute(), listOfTypes, index) ;
}

function addAttribute(parent, attribute, listOfTypes, index){
	var editBlock = document.createElement("span") ;
	editBlock.className = "editBlock" ;
	
		var attName = document.createElement("input") ;
		attName.setAttribute("type", "text") ;
		attName.setAttribute("id", "attribute_name_" + index) ;
		attName.setAttribute("name", "attribute_name_" + index) ;
		attName.setAttribute("value", attribute.name) ;
		editBlock.appendChild(attName) ;
		
		editBlock.appendChild(document.createTextNode(" ")) ;
		
		var types = document.createElement("select") ;
		types.setAttribute("id", "attribute_type_" + index) ;
		types.setAttribute("name", "attribute_type_" + index) ;
		types.className = "secondInput" ;
			for(var i=-1; i<listOfTypes.length; i++){
				
				var t = "" ;
				if(i>-1) t = listOfTypes[i] ;
				var type = document.createElement("option") ;
				type.setAttribute("value", t) ;
				if(t == attribute.type) type.setAttribute("selected", "selected") ; 
				type.innerHTML = t;
				
				types.appendChild(type) ;
			}
		editBlock.appendChild(types) ;
		
		parent.appendChild(editBlock) ;
}